package de.stefanocke.roo.wicket.details

import de.stefanocke.roo.wicket.Stereotypes
import de.stefanocke.roo.wicket.base.BeanPropertiesAwareItdBuilder
import de.stefanocke.roo.wicket.model.BeanPropertyMetadata
import java.lang.reflect.Modifier
import org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails
import org.springframework.roo.classpath.details.MethodMetadata
import de.stefanocke.roo.wicket.model.BeanPropertyMetadataWithDetails

import static de.stefanocke.roo.xtend.Method.*
import static de.stefanocke.roo.xtend.MethodSignature.*
import static de.stefanocke.roo.xtend.Parameter.*

import static extension de.stefanocke.roo.xtend.RooExtensions.*

class DetailsItdBuilder extends BeanPropertiesAwareItdBuilder<DetailsComponentMetadata>{
	
	override DetailsMetadata getMetadata(){super.metadata as DetailsMetadata} 
	
	override String baseClassStereotype(){
		Stereotypes::DETAILS	
	}
	
	override getMethods()
	{
			var methods = constructors
			methods += getCreateComponentMethods(propertiesForScaffolding) 
			methods += getCreateFieldsMethod
			methods += getObjectClassMethod
			methods
	}
	
	def getConstructors(){
		newArrayList(
			constructorWithId,
			constructorWithIdAndModel,
			constructorWithIdAndModelAndServiceBindings
		)
	}
	
	//	public KontaktForm(String id, IModel<Kontakt> model,
	//			IServiceBindings<Kontakt, ?> serviceBindings) {
	//		super(id, model, serviceBindings);
	//	}
	def constructorWithIdAndModelAndServiceBindings(){
		method(
			signature(Modifier::PUBLIC, 
				param("java.lang.String", "id"), 
				modelParam(modelClass, "model"),
				param(Stereotypes::IENTITYBINDINGS.stereotype(modelClass, WILDCARD), "serviceBindings")
			),
			[|'''
			super(id, model, serviceBindings);
			''']
		)
	}
	
	def getCreateFieldsMethod(){
		val returnType = "java.util.List".javaType("org.apache.wicket.Component")
		method(
			signature(
				Modifier::PUBLIC, returnType, "createFields"
			),
			[|'''
			«returnType.resolve» fields = new «"java.util.ArrayList".javaType("org.apache.wicket.Component").resolve»();
			«FOR p : propertiesForScaffolding»
				fields.add(«p.createComponentMethodName»());
			«ENDFOR»
			return fields;
			''']
			
		)
	}
	
	
	override getCreateComponentMethodName(BeanPropertyMetadata p){
		"create"+p.getPropertyName.symbolNameCapitalisedFirstLetter+"Field"
	}
	
	override ClassOrInterfaceTypeDetails getListComponentClass(BeanPropertyMetadataWithDetails p){
		Stereotypes::LIST_VIEW.stereotypeDetails
	}
	
	override MethodMetadata getListComponentMethodToOverride(ClassOrInterfaceTypeDetails listComponentClass){
		listComponentClass.getMethod("createItemComponent".symbol) //TODO: Stereotype Annotation on the method? 
	}
	

	
}